package com.tuyrt.`as`.proj.main.biz.account

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import com.alibaba.android.arouter.facade.annotation.Route
import com.tuyrt.`as`.proj.common.ui.component.HiBaseActivity
import com.tuyrt.`as`.proj.main.R
import com.tuyrt.`as`.proj.main.http.ApiFactory
import com.tuyrt.`as`.proj.main.http.api.AccountApi
import com.tuyrt.hi.library.restful.HiCallback
import com.tuyrt.hi.library.restful.HiResponse
import com.tuyrt.hi.library.util.HiStatusBar
import kotlinx.android.synthetic.main.activity_registration.*

/**
 * author: Administrator on 2021/6/28.
 * desc:
 *  测试：
 *  imoocId：5733850
 *  orderId：3359
 */
@Route(path = "/account/registration")
class RegistrationActivity : HiBaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_registration)
        HiStatusBar.setStatusBar(this, true, translucent = false)

        action_back.setOnClickListener { onBackPressed() }

        action_submit.setOnClickListener { submit() }
    }

    private fun submit() {
        val orderId = input_item_orderId.getEditText().text.trim().toString()
        val moocId = input_item_moocId.getEditText().text.trim().toString()

        val username = input_item_username.getEditText().text.trim().toString()
        val pwd = input_item_password.getEditText().text.trim().toString()
        val pwdSec = input_item_pwd_check.getEditText().text.trim().toString()

        if (TextUtils.isEmpty(orderId)
            || TextUtils.isEmpty(moocId)
            || TextUtils.isEmpty(username)
            || TextUtils.isEmpty(pwd)
            || (!TextUtils.equals(pwd, pwdSec))
        ) {
            return
        }

        ApiFactory.create(AccountApi::class.java)
            .register(username, pwd, moocId, orderId)
            .enqueue(object : HiCallback<String> {
                override fun onSuccess(response: HiResponse<String>) {
                    if (response.code == HiResponse.SUCCESS) {
                        // 注册成功
                        val intent = Intent()
                        intent.putExtra("username", username)
                        setResult(Activity.RESULT_OK, intent)
                        finish()
                    } else {
                        toast(response.msg)
                    }
                }

                override fun onFailed(throwable: Throwable) {
                    toast(throwable.message)
                }
            })
    }


}